<template>
  <div id="login">
    <div class="pannel">
      <div class="pannel-title">
        后台管理系统
      </div>
      <div class="pannel-body">
        <el-form :model="form" :rules="rules" ref="form">
          <el-form-item prop="username">
            <el-input
              v-model="form.username"
              placeholder="请输入账号"
              autocomplete="false"
              clearable
            >
              <i slot="prefix" class="el-input__icon el-icon-user"></i>
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model="form.password"
              placeholder="请输入密码"
              type="password"
              clearable
            >
              <i slot="prefix" class="el-input__icon el-icon-key"></i>
            </el-input>
          </el-form-item>
          <el-form-item class="button">
            <el-button type="primary" @click="login">登 陆</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import { login } from "@/api/account";
export default {
  data() {
    return {
      form: {
        username: "",
        password: ""
      },
      rules: {
        username: [{ required: true, message: "请输入账号", trigger: "blur" }],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }]
      }
    };
  },
  methods: {
    login() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          login(this.form).then(res => {
            if (res.data.code === "200") {
              this.$message({
                message: "登陆成功",
                type: "success"
              });
              this.$router.replace("/");
            } else {
              this.$message({
                message: res.data.msg,
                type: "error"
              });
              this.form.password = "";
            }
          });
        }
      });
    }
  }
};
</script>

<style scoped lang="scss">
#login {
  padding-top: 15vh;
  .pannel {
    padding-top: 100px;
    padding-bottom: 100px;
    background: #0080c0;
    .pannel-title {
      padding: 20px 0;
      height: 60px;
      color: #fff;
      font-size: 26px;
      line-height: 60px;
      text-align: center;
    }
    .pannel-body {
      background: #fff;
      border-radius: 15px;
      padding: 50px;
      padding-bottom: 20px;
      width: 300px;
      margin: 0 auto;
    }
  }
  .button {
    text-align: center;
  }
}
</style>
